package com.fjb.controller.sms;

import java.time.LocalDateTime;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fjb.common.aliyun.AliYunConfig;
import com.fjb.common.aliyun.AliYunSmsConfig;
import com.fjb.common.entity.HttpCode;
import com.fjb.common.entity.JsonResult;
import com.fjb.common.global.GlobalConfig;
import com.fjb.controller.base.BaseController;
import com.fjb.dto.LoginUserInfo;
import com.fjb.pojo.log.SysSmsCodeLog;
import com.fjb.pojo.user.SysUser;
import com.fjb.service.log.SysSmsCodeLogService;
import com.fjb.service.user.SysUserService;

/**
 * @Description:短信
 * @author hemiao
 * @time:2020年2月25日 下午5:42:41
 */
@Controller
@RequestMapping("/smsCode")
public class SmsCodeController extends BaseController{
	
	private static final Logger logger = LoggerFactory.getLogger(SmsCodeController.class);
	
	@Autowired
	private SysUserService sysUserService;
	@Autowired
	private SysSmsCodeLogService sysSmsCodeLogService;
	
	/**
	 * @Description:绑定手机号 验证码
	 * @param request
	 * @return
	 * JsonResult<Boolean>
	 * @exception:
	 * @author: hemiao
	 * @time:2020年2月25日 下午5:44:30
	 */
	@RequestMapping(value="/bangDingPhone")
	@ResponseBody
	public JsonResult<Boolean> bangDingPhone(HttpServletRequest request){
		HttpCode httpCode = HttpCode.PARAM_VERIFICATION;
		String phone = request.getParameter("phone");
		if(StringUtils.isBlank(phone)) {
			httpCode.setMsg("手机号不能为空");
			return new JsonResult<Boolean>(null, httpCode);
		}
		// 必须登录
		LoginUserInfo userInfo = getBaseWxTokenId(request);
		Integer userId = userInfo.getUserId();
		
		// 查询手机号是否绑定
		QueryWrapper<SysUser> queryWrapper = new QueryWrapper<SysUser>();
		queryWrapper.eq("phone", phone);
		SysUser sysUser = sysUserService.getOne(queryWrapper);
		if(sysUser!=null) {	
			httpCode.setMsg("手机号已被绑定");
			return new JsonResult<Boolean>(null, httpCode);
		}
		// 没有绑定可以发送短信
		SendSmsResponse smsResponse = new SendSmsResponse();
		try {
			smsResponse = AliYunSmsConfig.sendSms(phone, AliYunSmsConfig.SIGN_NAME, AliYunSmsConfig.VERIFCATION_CODE);
		} catch (ClientException e) {
			logger.info(" 绑定手机号 验证码异常    phone = " +phone);
			e.printStackTrace();
		}
		// 保存验证码
		int configSwitch = GlobalConfig.DEVELOP_CONFIG_SWITCH;
		SysSmsCodeLog scl = new SysSmsCodeLog();
		scl.setUserId(userId);
		String code = smsResponse.getCode();
		if(StringUtils.isBlank(code)) {
			code = "123456";
		}
		scl.setSmsCode(code);
		scl.setConfigSwitch(String.valueOf(configSwitch));
		scl.setCreateUserId(userId);
		scl.setCreateTime(LocalDateTime.now());	
		scl.setSmsStatus(1);
		sysSmsCodeLogService.save(scl);
		return new JsonResult<Boolean>(null, HttpCode.SUCCESS);
	}
	
}
